package GUI_Data.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * New-style button
 *
 * @author Xin Wang
 * @version 1.0
 * @since 5/26 2022
 */
public class NewButton extends JButton {
    private Icon defIcon;
    private Icon pressedIcon;
    private Icon hoverIcon;
    private MouseListener mouseListener;

    private String buttonText;
    private NewButton newButton;
    private Color defColor;
    private Color hoverColor;
    private Color clickColor;
    private int xAxis;

    /**
     * Constructor.
     * @param text of the button.
     */
    public NewButton(String text, String colorClass, int type){
        newButton=this;
        this.buttonText=text;
        this.setContentAreaFilled(false);
        this.setBorderPainted(false);

        this.setFocusPainted(false);

        switch (type){
            case 1: this.xAxis=3;//bookingNumber
            break;
            case 2: this.xAxis=100;//Exit
            break;
            case 3: this.xAxis=5;//origin
            break;
            case 4: this.xAxis=30;//keyboard
            break;
        }

        switch (colorClass) {
            case "grey" -> {
                defColor = Color.gray;
                hoverColor = new Color(100, 100, 100);
                clickColor = Color.darkGray;
            }
            case "green" -> {
                defColor = new Color(84, 139, 84);
                hoverColor = new Color(46, 139, 87);
                clickColor = new Color(0, 139, 69);
            }
            case "red" -> {
                defColor = new Color(165, 42, 42);
                hoverColor = new Color(139, 35, 35);
                clickColor = new Color(139, 26, 26);
            }
            case "white" ->{
                defColor =  Color.lightGray;
                hoverColor = Color.lightGray;
                clickColor =Color.lightGray;
            }
        }
        generateIcon();

        this.setIcon(defIcon);
        this.setPressedIcon(pressedIcon);
        this.addMouseListener(mouseListener);

    }

    /**
     * Change the color of the button
     * @param colorClass String: "grey", "green", "red" or "noColor".
     */
    public void changeColor(String colorClass){
        switch (colorClass) {
            case "grey" -> {
                defColor = Color.gray;
                hoverColor = new Color(100, 100, 100);
                clickColor = Color.darkGray;
            }
            case "green" -> {
                defColor = new Color(84, 139, 84);
                hoverColor = new Color(46, 139, 87);
                clickColor = new Color(0, 139, 69);
            }
            case "red" -> {
                defColor = new Color(165, 42, 42);
                hoverColor = new Color(139, 35, 35);
                clickColor = new Color(139, 26, 26);
            }
            case "noColor" ->{
                defColor = new Color(255, 255, 255);
                hoverColor = new Color(255, 255, 255);
                clickColor = new Color(255, 255, 255);
            }
        }
        generateIcon();
        this.setIcon(defIcon);
        this.setPressedIcon(pressedIcon);
        this.addMouseListener(mouseListener);
    }


    /**
     * Generate  defIcon, pressedIcon and hoverIcon
     */
    private void generateIcon(){

        defIcon = new Icon()
        {
            @Override
            public void paintIcon(Component cmp, Graphics g, int width, int height)
            {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(defColor);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.fillRoundRect(0, 0, newButton.getPreferredSize().width, newButton.getPreferredSize().height, 15, 15);
                g2d.setColor(Color.white);
                Font font = new Font("Arial", Font.PLAIN, 30);
                g2d.setFont(font);
                g2d.drawString(buttonText, xAxis, Math.round(newButton.getPreferredSize().height*0.58));
            }

            @Override
            public int getIconWidth()
            {
                return 100;
            }

            @Override
            public int getIconHeight()
            {
                return 30;
            }
        };


        pressedIcon = new Icon()
        {
            @Override
            public void paintIcon(Component c, Graphics g, int x, int y)
            {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(clickColor);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.fillRoundRect(0, 0, newButton.getWidth(), newButton.getHeight(), 15, 15);
                g2d.setColor(Color.white);
                Font font = new Font("Arial", Font.PLAIN, 30);
                g2d.setFont(font);
                g2d.drawString(buttonText,xAxis, Math.round(newButton.getPreferredSize().height*0.58));
            }

            @Override
            public int getIconWidth()
            {
                return 100;
            }

            @Override
            public int getIconHeight()
            {
                return 30;
            }
        };


         hoverIcon = new Icon()
        {
            @Override
            public void paintIcon(Component c, Graphics g, int x, int y)
            {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(hoverColor);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.fillRoundRect(0, 0, newButton.getWidth(), newButton.getHeight(), 15, 15);
                g2d.setColor(Color.white);
                Font font = new Font("Arial", Font.PLAIN, 30);
                g2d.setFont(font);
                g2d.drawString(buttonText, xAxis, Math.round(newButton.getPreferredSize().height*0.58));
            }

            @Override
            public int getIconWidth()
            {
                return 100;
            }

            @Override
            public int getIconHeight()
            {
                return 30;
            }
        };

        mouseListener = new MouseListener()
        {

            @Override
            public void mouseReleased(MouseEvent e)
            {

            }

            @Override
            public void mousePressed(MouseEvent e)
            {

            }

            @Override
            public void mouseExited(MouseEvent e)
            {
                newButton.setIcon(defIcon);
            }

            @Override
            public void mouseEntered(MouseEvent e)
            {
                newButton.setIcon(hoverIcon);
            }

            @Override
            public void mouseClicked(MouseEvent e)
            {

            }
        };

    }





}
